<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Ekskurs extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
		// Call the Model constructor
		parent::_construct();
	}
	function show($page,$limit,$sidx,$sord){
		$this->load->database();

		$result = $this->db->query("SELECT COUNT(*) AS count FROM ekskurs");
		if ($result->num_rows() > 0)
		{
   			$row = $result->row();
			$count = $row->count;
		}
		else
		{
			$count=0;
		}

		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		$SQL = "SELECT * FROM ekskurs ORDER BY $sidx $sord LIMIT $start , $limit";
		//$this->log($SQL);
		//mysql_freeresult($result);
		$result = $this->db->query($SQL);

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i=0;

		foreach($result->result_array() as $row)
		{
			$responce->rows[$i]['id']=$row['id'];
			$responce->rows[$i]['cell']=array($row['id'],
											  $row['ekskursTitle'],
											  $row['ekskursDescription']
												);
		    $i++;
		}
		return json_encode($responce);
	}
	function edit($id,$name,$desc)
	{
		$this->load->database();
		$sql = "UPDATE ekskurs SET 	ekskursTitle='$name',
									ekskursDescription='$desc'
									WHERE id=$id";
		//$this->log($sql);
		$this->db->query($sql);
	}
	function del($Id)
	{
		$this->load->database();
		$sql = "DELETE FROM ekskurs WHERE id=$Id";
		$this->db->query($query);
	}
	function addtour($name,$desc)
	{
		$this->load->database();
		str_replace(":","",$name);
		str_replace(";","",$name);
		$sql="INSERT INTO ekskurs(`ekskursTitle`,
									`ekskursDescription`
									) VALUES(
									'$name',
									'$desc'
									)";
		$this->db->query($sql);
	}
	function js()
	{
		$this->load->database();
		$SQL = "SELECT id,ekskursTitle FROM ekskurs ";
		$result = $this->db->query($SQL);
			$responce="0:";
		foreach($result->result_array() as $row)
		{
			$responce.=";".$row['id'].":".  $row['ekskursTitle'];
		}
		return $responce;
	}
	function showSubGrid($id)
	{
		$this->load->database();
		$query="Select ekskurs from idTE where tour=".$id;
		$resp=$this->db->query($query);
		$i=0;
		foreach($resp->result() as $row)
		{
			$query="Select ekskursTitle from ekskurs where id=".$row->ekskurs;
			$r=$this->db->query($query);
			if ($r->num_rows()>0)
			$eks = $r->row()->ekskursTitle;
			$responce->rows[$i]['cell']=array($eks,$eks);
			$i++;
		}
		return json_encode($responce);
	}
}